package pshop.servlet.util;

import pshop.Bestellung;
import pshop.Person;
import pshop.servlet.util.EmailTextUtils;
import pshop.servlet.util.EmailUtils;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

public class EmailUtils
{
   public static void sendBestMail(Bestellung best, HttpServletRequest req)
   {
      Person kunde = best.getKunde();

      Properties props = new Properties();
      Session session = Session.getDefaultInstance(props, null);

      try
      {
         String msgBody = EmailTextUtils.generateBestText(best, req);
         Message msg = new MimeMessage(session);
         msg.setFrom(new InternetAddress("bestellung@pfeffersprayshop.de", "PfefferSprayShop.de Bestellung"));
         msg.addRecipient(Message.RecipientType.TO, new InternetAddress(kunde.getEmail(), kunde.getVorname() + " "
            + kunde.getName()));
         msg.setSubject("Ihre Bestellung bei PfefferSprayShop.de");
         msg.setText(msgBody);
         Transport.send(msg);
         
         
         
      }
      catch (Exception e)
      {
         Log.logError(e);
      }

   }
}